<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head>

<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>
                <!-- vue事件绑定，如果函数加了括号就可以传参了，不加括号也可以 -->
                <input type="button" value="添加" class="btn btn-primary" @click="add()">
                <label>
                    搜索关键字:
                    <input type="text" class="form-control" v-model="keywords">
                </label>
            </div>
        </div>

        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>ctime</th>
                    <th>operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for='item in search(keywords)' :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime}}</td>
                    <td>
                        <a href="#" @click.prevent="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>

    </div>
    <script>
        //创建vue实例
        var vm = new Vue({
            el: '#app',
            data: {
                id: '',
                name: '',
                keywords: '',
                list: [
                    { id: 1, name: '奔驰', ctime: new Date() },
                    { id: 2, name: '宝马', ctime: new Date() }
                ]
            },
            methods: {
                add() { //添加品牌的方法
                    var car = { id: this.id, name: this.name, ctime: new Date() };
                    this.list.push(car);
                    this.id = this.name = '';
                },
                del(id) { //删除品牌
                    // 数组的循环
                    // 使用some删除
                    // this.list.some((item,i)=>{
                    //     if(item.id==id){
                    //         // 根据id删除数组元素
                    //         this.list.splice(i,1);
                    //         // 数组的some方法，如果返回ture表示立即终止循环
                    //         return true;
                    //     }
                    // })

                    // 使用findIndex删除
                    var index = this.list.findIndex(item => {
                        if (item.id == id) {
                            return true;
                        }
                    })
                    // 删除
                    this.list.splice(index, 1);

                },
                search(keywords) { //根据关键词过滤
                    // 方法1
                    // var newList = [];
                    // this.list.forEach(element => {
                    //     if (element.name.indexOf(keywords) != -1 || element.id == keywords || element.id.toString().indexOf(keywords)!=-1) {
                    //         newList.push(element);
                    //     }

                    // });
                    // return newList;
                    // 方法2
                    var newList = this.list.filter(item => {
                        if (item.name.includes(keywords) || item.id.toString().includes(keywords)) {
                            return item;
                        }
                    })
                    return newList;
                }

            }
        })
    </script>
</body>

</html>